/*
 * main.c
 *
 *  Created on: 2012/11/18
 *      Author: Leon
 *
 *  This is a simple protothread porting on MSP430G2553 on MSP430 Launchpad.
 *
 *  Project reference and modification from MSP430 Launchpad LED blink example code.
 *
 */
/*
 * ======== Standard MSP430 includes ========
 */
#include <msp430.h>

/*
 *  Include RTOS header
 */
#include "pt-1.4/pt.h"

/*
 * ======== Grace related includes ========
 */
#include <ti/mcu/msp430/csl/CSL.h>

/*
 * Include Driver
 */
#include "Drivers/MSP430_SysTimer.h"

/*
 * Global variable
 */
static struct pt pState1, pState2;




/*
 * Testing thread1
 */
static PT_THREAD(pThread1(struct pt *pt))
{
	static unsigned long timer = 0;
	PT_BEGIN(pt);

	while(1) {
		/* Let the other protothread run. */
		timer = MSP430_SysTimer_GetTick();

		/* Wait until the other protothread has set its flag. */
		PT_WAIT_UNTIL(pt, (MSP430_SysTimer_GetTick() > timer + 500));
		/* toogle LED */
		P1OUT ^= BIT0;

		/* And we loop. */
	}
	PT_END(pt);
}
/*
 * Testing thread2
 */
static PT_THREAD(pThread2(struct pt *pt))
{
	static unsigned long timer = 0;
	PT_BEGIN(pt);

	while(1) {
		/* Let the other protothread run. */
		timer = MSP430_SysTimer_GetTick();

		/* Wait until the other protothread has set its flag. */
		PT_WAIT_UNTIL(pt, (MSP430_SysTimer_GetTick() > timer + 100));
		/* toogle LED */
		P1OUT ^= BIT6;

		/* And we loop. */
	}
	PT_END(pt);
}
/*
 *  ======== main ========
 */
void main(void)
{
    CSL_init();                     // Activate Grace-generated configuration

    /* Initialize the protothread state variables with PT_INIT(). */
	PT_INIT(&pState1);
	PT_INIT(&pState2);

	while(1){
		pThread1(&pState1);
		pThread2(&pState2);
	}

}

/*
 *  default ISR vector
 */
#pragma vector=	PORT1_VECTOR,\
				PORT2_VECTOR,\
				ADC10_VECTOR,\
				USCIAB0TX_VECTOR,\
				USCIAB0RX_VECTOR,\
				TIMER0_A1_VECTOR,\
				WDT_VECTOR,\
				COMPARATORA_VECTOR,\
				TIMER1_A1_VECTOR,\
				TIMER1_A0_VECTOR,\
				NMI_VECTOR
__interrupt void ISR_trap(void)
{
	while(1);
}
